package o;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import o.hg.c;
import o.hg.e;

@RestrictTo({d.LIBRARY})
public final class fd<T> {
    private final e<ArrayList<T>> ˊ = new c(10);
    private final ArrayList<T> ˎ = new ArrayList();
    private final dy<T, ArrayList<T>> ˏ = new dy();
    private final HashSet<T> ॱ = new HashSet();

    public void ˋ(@NonNull T t) {
        if (!this.ˏ.containsKey(t)) {
            this.ˏ.put(t, null);
        }
    }

    public boolean ˎ(@NonNull T t) {
        return this.ˏ.containsKey(t);
    }

    public void ˊ(@NonNull T t, @NonNull T t2) {
        if (this.ˏ.containsKey(t) && this.ˏ.containsKey(t2)) {
            ArrayList arrayList = (ArrayList) this.ˏ.get(t);
            if (arrayList == null) {
                arrayList = ˋ();
                this.ˏ.put(t, arrayList);
            }
            arrayList.add(t2);
            return;
        }
        throw new IllegalArgumentException("All nodes must be present in the graph before being added as an edge");
    }

    @Nullable
    public List ॱ(@NonNull T t) {
        return (List) this.ˏ.get(t);
    }

    @Nullable
    public List<T> ˊ(@NonNull T t) {
        List<T> list = null;
        int size = this.ˏ.size();
        for (int i = 0; i < size; i++) {
            ArrayList arrayList = (ArrayList) this.ˏ.ˋ(i);
            if (arrayList != null && arrayList.contains(t)) {
                if (list == null) {
                    arrayList = new ArrayList();
                } else {
                    List list2 = list;
                }
                arrayList.add(this.ˏ.ॱ(i));
                list = arrayList;
            }
        }
        return list;
    }

    public boolean ˏ(@NonNull T t) {
        int size = this.ˏ.size();
        for (int i = 0; i < size; i++) {
            ArrayList arrayList = (ArrayList) this.ˏ.ˋ(i);
            if (arrayList != null && arrayList.contains(t)) {
                return true;
            }
        }
        return false;
    }

    public void ˎ() {
        int size = this.ˏ.size();
        for (int i = 0; i < size; i++) {
            ArrayList arrayList = (ArrayList) this.ˏ.ˋ(i);
            if (arrayList != null) {
                ˏ(arrayList);
            }
        }
        this.ˏ.clear();
    }

    @NonNull
    public ArrayList<T> ˊ() {
        this.ˎ.clear();
        this.ॱ.clear();
        int size = this.ˏ.size();
        for (int i = 0; i < size; i++) {
            ˎ(this.ˏ.ॱ(i), this.ˎ, this.ॱ);
        }
        return this.ˎ;
    }

    private void ˎ(T t, ArrayList<T> arrayList, HashSet<T> hashSet) {
        if (!arrayList.contains(t)) {
            if (hashSet.contains(t)) {
                throw new RuntimeException("This graph contains cyclic dependencies");
            }
            hashSet.add(t);
            ArrayList arrayList2 = (ArrayList) this.ˏ.get(t);
            if (arrayList2 != null) {
                int size = arrayList2.size();
                for (int i = 0; i < size; i++) {
                    ˎ(arrayList2.get(i), arrayList, hashSet);
                }
            }
            hashSet.remove(t);
            arrayList.add(t);
        }
    }

    @NonNull
    private ArrayList<T> ˋ() {
        ArrayList<T> arrayList = (ArrayList) this.ˊ.ˎ();
        if (arrayList == null) {
            return new ArrayList();
        }
        return arrayList;
    }

    private void ˏ(@NonNull ArrayList<T> arrayList) {
        arrayList.clear();
        this.ˊ.ˋ(arrayList);
    }
}
